<?php
/**
 * Created by PhpStorm.
 * User: zhj
 * Date: 2019/3/6
 * Time: 16:39
 */


namespace app\common\logic;

/**
 * 文化逻辑
 */
class Culture extends LogicBase
{

    /**
     * 文化分类编辑
     */
    public function cultureCategoryEdit($data = [])
    {

        $validate_result = $this->validateCultureCategory->scene('edit')->check($data);

        if (!$validate_result) {

            return [RESULT_ERROR, $this->validateCultureCategory->getError()];
        }

        $url = url('cultureCategoryList');

        $result = $this->modelCultureCategory->setInfo($data);

        $handle_text = empty($data['id']) ? '新增' : '编辑';

        $result && action_log($handle_text, '文化分类' . $handle_text . '，name：' . $data['name']);

        return $result ? [RESULT_SUCCESS, '操作成功', $url] : [RESULT_ERROR, $this->modelCultureCategory->getError()];
    }

    /**
     * 获取文化列表
     */
    public function getCultureList($where = [], $field = '*', $order = '')
    {

       /* $this->modelCulture->alias('a');

        $join = [
            [SYS_DB_PREFIX . 'member m', 'a.member_id = m.id'],
            [SYS_DB_PREFIX . 'culture_category c', 'a.category_id = c.id'],
        ];

        $where['a.' . DATA_STATUS_NAME] = ['neq', DATA_DELETE];

        $this->modelCulture->join = $join;*/

        return $this->modelCulture->getList($where, $field, $order);
    }

    /**
     * 获取文化列表搜索条件
     */
    public function getWhere($data = [])
    {

        $where = [];

        !empty($data['search_data']) && $where['a.name|a.describe'] = ['like', '%'.$data['search_data'].'%'];

        return $where;
    }

    /**
     * 文化信息编辑
     */
    public function cultureEdit($data = [])
    {

        $validate_result = $this->validateCulture->scene('edit')->check($data);

        if (!$validate_result) {

            return [RESULT_ERROR, $this->validateCulture->getError()];
        }

        $url = url('cultureList');

        empty($data['id']) && $data['member_id'] = MEMBER_ID;

        $data['content'] = html_entity_decode($data['content']);

        $result = $this->modelCulture->setInfo($data);

        $handle_text = empty($data['id']) ? '新增' : '编辑';

        $result && action_log($handle_text, '文化' . $handle_text . '，name：' . $data['name']);

        return $result ? [RESULT_SUCCESS, '文化操作成功', $url] : [RESULT_ERROR, $this->modelCulture->getError()];
    }

    /**
     * 获取文化信息
     */
    public function getCultureInfo($where = [], $field = '*')
    {

       /* $this->modelCulture->alias('a');

        $join = [
            [SYS_DB_PREFIX . 'member m', 'a.member_id = m.id'],
            [SYS_DB_PREFIX . 'culture_category c', 'a.category_id = c.id'],
        ];

        $where['a.' . DATA_STATUS_NAME] = ['neq', DATA_DELETE];

        $this->modelCulture->join = $join;*/

        return $this->modelCulture->getInfo($where, $field);
    }

    /**
     * 获取分类信息
     */
    public function getCultureCategoryInfo($where = [], $field = true)
    {

        return $this->modelCultureCategory->getInfo($where, $field);
    }

    /**
     * 获取文化分类列表
     */
    public function getCultureCategoryList($where = [], $field = true, $order = '', $paginate = 0)
    {

        return $this->modelCultureCategory->getList($where, $field, $order, $paginate);
    }

    /**
     * 文化分类删除
     */
    public function cultureCategoryDel($where = [])
    {

        $result = $this->modelCultureCategory->deleteInfo($where);

        $result && action_log('删除', '文化分类删除，where：' . http_build_query($where));

        return $result ? [RESULT_SUCCESS, '文化分类删除成功'] : [RESULT_ERROR, $this->modelCultureCategory->getError()];
    }

    /**
     * 文化删除
     */
    public function cultureDel($where = [])
    {

        $result = $this->modelCulture->deleteInfo($where);

        $result && action_log('删除', '文化删除，where：' . http_build_query($where));

        return $result ? [RESULT_SUCCESS, '文化删除成功'] : [RESULT_ERROR, $this->modelCulture->getError()];
    }
}
